
GrassMode = {
	CreateUnit = function()
		local ObjUnit = cObj:New()
		ObjUnit:SetShader("mesh.fx")
		ObjUnit:SetMesh("MeshPlaneUnit")
		ObjUnit:SetVisible(false)
		ObjUnit:SetCanSave(false)
		g_MgrObj:Add("ObjGrassUnit",ObjUnit)
		GrassMode.ObjUnit = ObjUnit;

		GrassMode.CurFuncSeq = GrassMode.FuncSeq.Pass1
		TRACE("GrassMode - GrassMode Created.\n")
	end,
	ShowUnit = function()
		GrassMode.ObjUnit:SetVisible(true)
	end,
	HideUnit = function()
		GrassMode.ObjUnit:SetVisible(false)
	end,
	UpdateUnit = function()
		local Min,Max = BoundBox(GrassMode.Point[1], GrassMode.Point[2])
		GrassMode.ObjUnit:SetPos({x=Min.x,y=0,z=Min.z})
		GrassMode.ObjUnit:SetScaling({x=Max.x-Min.x,y=1,z=Max.z-Min.z})
		GrassMode.Area.x = Max.x - Min.x
		GrassMode.Area.z = Max.z - Min.z
	end,
	FuncSeq = {
		Pass1 = function()
			if cWindow:MouseButtonDown(DIK.LBUTTON) then
				GrassMode.Point[1] = GetPickXZ()
				GrassMode.CurFuncSeq = GrassMode.FuncSeq.Pass2
				GrassMode.ObjUnit:SetVisible(true)
			end
		end,
		Pass2 = function()
			GrassMode.Point[2] = GetPickXZ()
			GrassMode.UpdateUnit()
			if not cWindow:MouseButtonDown(DIK.LBUTTON) then
				if Vector.LengthBetween(GrassMode.Point[1],GrassMode.Point[2]) < 0.5 then
					TRACE("GrassMode - GrassPlane is too small!\n")
					GrassMode.CurFuncSeq = GrassMode.FuncSeq.Pass1
					return
				end
				GrassMode.CurFuncSeq = GrassMode.FuncSeq.Pass3
			end
		end,
		Pass3 = function()
			local XY = cWindow:MouseXY()
			UILayout.Create(GrassProp)
			GrassProp.__SelfUI:NoCloseButton()
			GrassProp.__SelfUI:SetPos(XY.x-GrassProp.__SelfUI:GetWidth(),XY.y-GrassProp.__SelfUI:GetHeight())
			GrassProp.GrassAmount:SetText("5")
			GrassProp.GrassHeight:SetText("20")
			GrassProp.GrassWidth:SetText("20")
			GrassProp.Ok:SetOnClick(function ()
				local GrassAmount = GrassProp.GrassAmount:GetText()
				local GrassHeight = GrassProp.GrassHeight:GetText()
				local GrassWidth  = GrassProp.GrassWidth:GetText()
				GrassMode.GrassAmount = tonumber(GrassAmount)
				GrassMode.GrassHeight = tonumber(GrassHeight)
				GrassMode.GrassWidth  = tonumber(GrassWidth)
				GrassProp.__SelfUI:Close()
				GrassMode.ObjUnit:SetVisible(false)
				GrassMode.CurFuncSeq = GrassMode.FuncSeq.Pass4
			end)
			GrassProp.Cancel:SetOnClick(function ()
				GrassProp.__SelfUI:Close()
				GrassMode.ObjUnit:SetVisible(false)
				GrassMode.CurFuncSeq = GrassMode.FuncSeq.End
			end)
			GrassMode.GrassAmount = nil
			GrassMode.GrassHeight = nil
			GrassMode.GrassWidth = nil
			GrassMode.CurFuncSeq = IdleFunc
		end,
		Pass4 = function()
			GrassMode.FuncSeq.CreateGrass()
			GrassMode.CurFuncSeq = GrassMode.FuncSeq.End
		end,
		End = function()
			GrassMode.CurFuncSeq = GrassMode.FuncSeq.Pass1
			GrassMode.ObjUnit:SetVisible(false)
		end,
		CreateGrass = function()
			if GrassMode.GrassWidth and GrassMode.GrassHeight and GrassMode.GrassAmount>0 then
				local NewMesh = cMeshModel:New()
				NewMesh:CreateGrass(cWindow:GetDevice(),GrassMode.Area.x,GrassMode.Area.z,GrassMode.GrassWidth,GrassMode.GrassHeight,GrassMode.GrassAmount)
				local MeshName = GenMeshName()
				g_MgrMesh:Add(MeshName,NewMesh)
				local Obj = cObj:New()
				Obj:SetShader("mesh.fx")
				Obj:SetMesh(MeshName)
				Obj:SetPos(GrassMode.ObjUnit:GetPos())
				Obj:SetAmbient(GenRandColor())
				local ObjName = GenObjName()
				g_MgrObj:Add(ObjName,Obj)
				TRACE("GrassMode - New Grass ("..ObjName..") Added.\n")
			else
				TRACE("GrassMode - GrassProp Error\n")
			end
		end
	},
	Area = {},
	Point = {}
}
